package com.huawei.smartcity0102;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.FrameLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.gson.JsonObject;
import com.huawei.smartcity0102.all.F_All;
import com.huawei.smartcity0102.home.F_Home;
import com.huawei.smartcity0102.me.F_Me;
import com.huawei.smartcity0102.model.login;
import com.huawei.smartcity0102.model.user;
import com.huawei.smartcity0102.news.F_SJFX;
import com.huawei.smartcity0102.util.Http;
import com.huawei.smartcity0102.zh.F_ZH;

import okhttp3.RequestBody;

public class MainActivity extends AppCompatActivity {

    private FrameLayout fragment;
    public static BottomNavigationView bottom;
    public static com.huawei.smartcity0102.model.user.UserDTO user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        Http.token="eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjU5ZTYzNzFkLTgzMzMtNDRiOS1iYTgxLTRmZDliMzllMmVlNSJ9.8ThzAG9f766pfYquUY9n6FgX_LXKdHGRFb2nTnUsc4U9SeMfIDnbDhBT8Ka50a-Eqty02Xgr62SB5d_hYuU40A";
        if(ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)!= PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.CALL_PHONE,Manifest.permission.CAMERA,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE},100);
        }
        getSupportFragmentManager().beginTransaction().add(R.id.fragment,new F_Home()).commit();
        bottom.setOnNavigationItemSelectedListener(it->{
            switch (it.getItemId()){
                case R.id.home:
                    addw(new F_Home());
                    break;
                case R.id.all:
                    addw(new F_All());
                    break;
                case R.id.zh:
                    addw(new F_ZH());
                    break;
                case R.id.sjfx:
                    addw(new F_SJFX());
                    break;
                case R.id.me:
                    addw(new F_Me());
                    break;
            }
            return true;
        });
        new Thread(new Runnable() {
            @Override
            public void run() {
                JsonObject jsonObject=new JsonObject();
                jsonObject.addProperty("username","zyy");
                jsonObject.addProperty("password","12345");
                Http.token = Http.getInstance().get("/prod-api/api/login", RequestBody.create(Http.m,jsonObject.toString()), login.class, 2).getToken();
                user = Http.getInstance().get("/prod-api/api/common/user/getInfo", null, user.class, 1).getUser();
            }
        }).start();
    }
    public void addw(Fragment fragment){
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment,fragment).commit();
    }

    private void initView() {
        fragment = (FrameLayout) findViewById(R.id.fragment);
        bottom = (BottomNavigationView) findViewById(R.id.bottom);
    }
}